package com.example.accessingdatajpa.controller;

import com.example.accessingdatajpa.pojo.FirstPagePicture;
import com.example.accessingdatajpa.service.IFirstPagePicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@RequestMapping(path = "/app")
public class FirstPagePicController {
    @Autowired
    final private IFirstPagePicService firstPagePicService;

    public FirstPagePicController(IFirstPagePicService firstPagePicService) {
        this.firstPagePicService = firstPagePicService;
    }

    //插入首页轮播图片
    @PostMapping(path = "/first-page-upload-pic", produces = "image/png")
    @ResponseBody
    public void saveFirstPagePic(FirstPagePicture firstPagePicture, @RequestParam("file") MultipartFile file) throws IOException {
        firstPagePicService.insertFirstPagePic(firstPagePicture,file);
    }


    //根据id读取首页轮播图片
    @GetMapping(path = "/first-page-get-pic/{id}")
    @ResponseBody
    public void getFirstPagePic(@PathVariable("id") Long id, HttpServletResponse response) throws IOException {
        firstPagePicService.queryFirstPagePicById(id,response);
    }

    //根据id更换首页轮播图片
    @PutMapping(path = "/first-page-update-pic/{id}")
    @ResponseBody
    public void updateFirstPagePic(@PathVariable("id") Long id, @RequestParam("file") MultipartFile file) throws IOException {
        firstPagePicService.updateFirstPagePicById(id,file);
    }
}
